<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 头部操作栏 -->
            <div class="layui-form toolbar">
                <div class="layui-form-item">
                    <div class="layui-inline">
                       <!-- <button id="robotTaskDetailBtnAdd" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>
                        <button id="robotTaskDetailBtnExport" class="layui-btn icon-btn"><i class="layui-icon">&#xe67d;</i>导出</button>-->
                    </div>
                </div>
            </div>
            <!-- 表格 -->
            <table class="layui-table" id="robotTaskDetailTable" lay-filter="robotTaskDetailTable"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="robotTaskDetailTableBar">
    <!--<a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>-->
</script>

<!-- 表单弹窗 -->
<script type="text/html" id="robotTaskDetailForm">
    <form lay-filter="robotTaskDetailForm" class="layui-form model-form">
        <input name="id" type="hidden"/>
        <div class="layui-form-item">
            <label class="layui-form-label">创建者</label>
            <div class="layui-input-block">
                <input name="creator" placeholder="请输入创建者" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">修改者</label>
            <div class="layui-input-block">
                <input name="modifier" placeholder="请输入修改者" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">创建人</label>
            <div class="layui-input-block">
                <input name="creatorName" placeholder="请输入创建人" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">修改人</label>
            <div class="layui-input-block">
                <input name="modifierName" placeholder="请输入修改人" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">执行任务的agv</label>
            <div class="layui-input-block">
                <input name="agvId" placeholder="请输入执行任务的agv" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">执行任务的机械臂</label>
            <div class="layui-input-block">
                <input name="armId" placeholder="请输入执行任务的机械臂" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">任务模板</label>
            <div class="layui-input-block">
                <select name="taskId" id="taskId" lay-filter="taskId" lay-verify="required"
                        required></select>
            </div>
        </div>
        <div class="layui-form-item text-right">
            <!--<button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
            <button class="layui-btn" lay-filter="robotTaskDetailFormSubmit" lay-submit>保存</button>-->
        </div>
    </form>
</script>

<!-- js部分 -->
<script>
    layui.use(['layer', 'form', 'table', 'admin'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var admin = layui.admin;
        var upload = layui.upload;

        // 渲染表格
        var insTb = table.render({
            elem: '#robotTaskDetailTable',
            url: config.base + 'enterprise/robot/admin/robotTaskDetail/list',
            page: true,
            cellMinWidth: 100,
            cols: [[
                {type: 'checkbox'}
                ,{field: 'creatorName', sort: true, title: '执行人'}
                ,{field: 'taskId', sort: false, title: '任务模板',templet:function(d){
                        let map = '';
                        admin.reqSync('enterprise/robot/admin/robotTask/list', {id: d.taskId}, function (res) {
                            if (200 == res.code&&res.data.length!=0) {
                                map =  res.data[0].name;
                            }
                        }, 'GET', true);
                        return map;
                    }
                }
                ,{field: 'agvId', sort: false, title: '执行任务的agv',templet:function(d){
                        let map = '';
                        admin.reqSync('enterprise/robot/admin/robotAgv/list', {id: d.agvId}, function (res) {
                            if (200 == res.code&&res.data.length!=0) {
                                map =  res.data[0].name;
                            }
                        }, 'GET', true);
                        return map;
                    }
                }
                ,{field: 'armId', sort: false, title: '执行任务的机械臂',templet:function(d){
                        let map = '';
                        admin.reqSync('enterprise/robot/admin/robotArm/list', {id: d.armId}, function (res) {
                            if (200 == res.code&&res.data.length!=0) {
                                map =  res.data[0].name;
                            }
                        }, 'GET', true);
                        return map;
                    }
                }
                ,{field: 'agvFrom', sort: false, title: 'agv起点',templet:function(d){
                        let map = '';
                        admin.reqSync('enterprise/robot/admin/robotStation/list', {id: d.agvFrom}, function (res) {
                            if (200 == res.code&&res.data.length!=0) {
                                map =  res.data[0].name;
                            }
                        }, 'GET', true);
                        return map;
                    }
                }
                ,{field: 'agvTo', sort: false, title: 'agv目的点',templet:function(d){
                        let map = '';
                        admin.reqSync('enterprise/robot/admin/robotStation/list', {id: d.agvTo}, function (res) {
                            if (200 == res.code&&res.data.length!=0) {
                                map =  res.data[0].name;
                            }
                        }, 'GET', true);
                        return map;
                    }
                }
                ,{field: 'fromPoint', sort: false, title: '起点动作点位',templet:function(d){
                        let map = '';
                        admin.reqSync('enterprise/robot/admin/robotPoint/list', {id: d.fromPoint}, function (res) {
                            if (200 == res.code&&res.data.length!=0) {
                                map =  res.data[0].name;
                            }
                        }, 'GET', true);
                        return map;
                    }
                }
                ,{field: 'toPoint', sort: false, title: '目的点动作点位',templet:function(d){
                        let map = '';
                        admin.reqSync('enterprise/robot/admin/robotPoint/list', {id: d.toPoint}, function (res) {
                            if (200 == res.code&&res.data.length!=0) {
                                map =  res.data[0].name;
                            }
                        }, 'GET', true);
                        return map;
                    }
                }
                ,{field: 'createTime', sort: true, title: '执行时间'}
                ,{field: 'statusDesc', sort: false, title: '状态'}
                ,{align: 'center', toolbar: '#robotTaskDetailTableBar', title: '操作', width: 200}
            ]],
            done: function (res, curr, count) {
            }
        });

        // 工具条点击事件
        table.on('tool(robotTaskDetailTable)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') { //修改
                showEditModel(data);
            } else if (obj.event === 'del') { // 删除
                doDelete(obj);
            }
        });

        //监听排序
        table.on('sort(robotTaskDetailTable)', function(obj){
            table.reload('robotTaskDetailTable', {
                initSort: obj
                ,where: {
                    sort: obj.field
                    ,order: obj.type
                }
            });
        });

        // 添加按钮点击事件
        $('#robotTaskDetailBtnAdd').click(function () {
            showEditModel();
        });

        // 显示编辑弹窗
        function showEditModel(data) {
            admin.open({
                type: 1,
                area: '640px',
                offset: '65px',
                title: data ? '修改复合机器人任务详细记录' : '添加复合机器人任务详细记录',
                content: $('#robotTaskDetailForm').html(),
                success: function () {
                    if(!data){
                        admin.req('enterprise/robot/admin/robotTask/list', {limit: 9999}, function (res) {
                            if (200 == res.code) {
                                loadSelectHtml(res.data, "请选择任务模板", "taskId", 0);
                            }else {
                                layer.msg('获取任务模板失败', {icon: 2, time: 1000});
                            }
                        }, 'GET', true);
                    }

                    if(data){
                        admin.reqSync('enterprise/robot/admin/robotTask/list', {limit: 9999}, function (res) {
                            if (200 == res.code) {
                                loadSelectHtml(res.data, "请选择任务模板", "taskId", 0);
                            }else {
                                layer.msg('获取任务模板失败', {icon: 2, time: 1000});
                            }
                        }, 'GET', true);
                    }


                    form.val('robotTaskDetailForm', data);
                    // 表单提交事件
                    form.on('submit(robotTaskDetailFormSubmit)', function (d) {
                        layer.load(2);
                        admin.req(data ? 'enterprise/robot/admin/robotTaskDetail/update' : 'enterprise/robot/admin/robotTaskDetail/add', d.field, function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                layer.msg(res.msg, {icon: 1});
                                table.reload('robotTaskDetailTable');
                                layer.closeAll('page');
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, data ? 'PUT' : 'POST');
                        return false;
                    });
                }
            });
        }

        // 删除
        function doDelete(obj) {
            layer.confirm('确定要删除吗？', {
                offset: '65px',
                skin: 'layui-layer-admin'
            }, function (i) {
                layer.close(i);
                layer.load(2);
                admin.req('enterprise/robot/admin/robotTaskDetail/' + obj.data.id, {}, function (res) {
                    layer.closeAll('loading');
                    if (res.code == 200) {
                        layer.msg(res.msg, {icon: 1});
                        obj.del();
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'DELETE');
            });
        }

        // 导出excel
        $('#robotTaskDetailBtnExport').click(function () {
            var checkRows = table.checkStatus('robotTaskDetailTable');
            if (checkRows.data.length == 0) {
                layer.msg('请选择要导出的数据', {icon: 2});
            } else {
                table.exportFile(insTb.config.id, checkRows.data, 'xls');
            }
        });

    });
</script>